package de.betagames.dreamworld.charactereditor.view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import de.betagames.dreamworld.main.Main;

public class CharacterChooser extends JFrame {

	private JComboBox<String> chars;
	private CharacterEditor ce;

	public CharacterChooser(final CharacterEditor ce) {
		this.ce = ce;
		setTitle("DreamWorld - Dream-Ego");
		chars = new JComboBox<String>();
		fillComboBox();
		setLayout(new GridLayout(3, 1));
		add(new JLabel("Choose a Dream-Ego:"));
		add(chars);
		add(getButtons());
		setResizable(false);
		setSize(320, 150);
		setLocationRelativeTo(null);
		setVisible(true);
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowListener() {

			@Override
			public void windowOpened(WindowEvent e) {

			}

			@Override
			public void windowIconified(WindowEvent e) {

			}

			@Override
			public void windowDeiconified(WindowEvent e) {

			}

			@Override
			public void windowDeactivated(WindowEvent e) {

			}

			@Override
			public void windowClosing(WindowEvent e) {
				Object[] options = { "Yes, please", "No, thanks", "Abort" };
				int n = JOptionPane
						.showOptionDialog(getParent(),
								"Do you want to go back to the menu?",
								"Close?", JOptionPane.YES_NO_CANCEL_OPTION,
								JOptionPane.QUESTION_MESSAGE, null, options,
								options[2]);

				if (n == JOptionPane.NO_OPTION) {
					System.exit(0);
				} else if (n == JOptionPane.YES_OPTION) {
					new Main();
					ce.dispose();
					dispose();
				}
			}

			@Override
			public void windowClosed(WindowEvent e) {

			}

			@Override
			public void windowActivated(WindowEvent e) {

			}
		});

	}

	private JPanel getButtons() {

		JPanel panel = new JPanel();

		panel.setLayout(new GridLayout(1, 2, 5, 5));
		JButton b1 = new JButton("New");
		JButton b2 = new JButton("OK");
		panel.add(b1);
		panel.add(b2);

		b1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				newCharacter();
			}
		});

		b2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				continueToMain();
			}
		});

		return panel;

	}

	private void continueToMain() {
		if (chars.getSelectedItem() != null) {
			ce.setChar(chars.getSelectedItem().toString());
			dispose();
		}
	}

	private void newCharacter() {
		setEnabled(false);
		new NewCharacterView(this);
	}

	public void fillComboBox() {
		setEnabled(true);
		chars.removeAllItems();
		File f = new File(System.getProperty("user.dir") + "/Characters/");

		f = new File(f.getAbsolutePath());
		String[] fileNames = f.list();

		for (String s : fileNames) {
			if (s.endsWith(".char")) {
				String item = s.substring(0, s.indexOf("."));
				chars.addItem(item);
			}
		}
	}
}
